#!/bin/bash
echo "===== 开始日志处理服务修复 ====="
# 记录开始时间
start_time=$(date +%s.%N)
# 设置变量
LOGSTASH_LOG="/var/log/logstash/logstash-plain.log"
TUNE_PROFILE=" latency-performance "
SYSCTL_PARAM="fs.file-max=655360; fs.inode-max=655360"
# 检查磁盘空间
DISK_USAGE=$(df -h / | grep "/dev" | awk '{print $4}' | tr -cd '[:digit:].')
if [ $(echo "$DISK_USAGE" | awk '{print $1 * 1024}') -le 1024 ]; then
    echo "磁盘空间不足，建议清理系统"
fi
# 权限检查和修复
chmod -R 644 $LOGSTASH_LOG
chown logstash:logstash $LOGSTASH_LOG
# 使用 tuned 进行调优
tuned-profiles
if [ "$TUNE_PROFILE" != "current profile" ]; then
    tuned-adm profile latency-performance
fi
# 使用 sysctl 调整参数
sysctl -p
for param in $(echo $SYSCTL_PARAM | tr ';' '\n'); do
    if ! sysctl -q $param; then
        echo "$param 设置失败"
    fi
done
# 收集指标和验证
DISK_IO=$(iostat -d -k | awk '/sda/ {print $3}')
MEMORY_USAGE=$(free -m | awk '/Mem:/ {print $3}')
CPU_LOAD=$(top -bn1 | grep load | awk '{print $(NF-2)}')
# 记录结束时间
end_time=$(date +%s.%N)
duration=$(echo "$end_time - $start_time" | bc)
echo "===== 调优摘要 ====="
echo "总执行时间: ${duration}秒"
echo "磁盘IO: ${DISK_IO}KB/s"
echo "内存使用量: ${MEMORY_USAGE}MB"
echo "CPU负载: ${CPU_LOAD}%"
# 结构化输出
echo "===== 系统状态 ====="
echo '{
    "total_time": "'$duration'",
    "disk_io": "'$DISK_IO'",
    "memory_usage": "'$MEMORY_USAGE'",
    "cpu_load": "'$CPU_LOAD'"
}'